//
//  SelectWeekViewController.m
//  JiuLe
//
//  Created by 黄鑫 on 16/9/1.
//  Copyright © 2016年 Fine. All rights reserved.
//

#import "SelectWeekViewController.h"
#import "CaseHistoryModel.h"

@interface SelectWeekViewController ()
@property (strong, nonatomic) NSMutableArray *arrDataSource;
@end

@implementation SelectWeekViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"重复";
    [self showNavgationWithLeftTiele:@"取消" andleftBtnAction:@selector(clearAction) WithRightTiele:@"保存" andrigheBtnAction:@selector(sureAction)];
}

- (void)clearAction{
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma  mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrDataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"selectWeekCell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"selectWeekCell"] ;
    }
    UILabel *labName = [cell.contentView viewWithTag:1];
    UIImageView *image = [cell.contentView viewWithTag:2];
    CaseHistoryModel *model = _arrDataSource[indexPath.row];
    labName.text = model.name;
    if (model.select) {
        image.hidden = NO;
    }else{
        image.hidden = YES;
    }
    return cell;
}

#pragma mark - UITableview Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CaseHistoryModel *model = _arrDataSource[indexPath.row];
    model.select = !model.select;
    [_arrDataSource replaceObjectAtIndex:indexPath.row withObject:model];
    [self.tableView reloadData];
}

#pragma mark - set/get
- (NSMutableArray *)arrDataSource{
    if (_arrDataSource == nil) {
        _arrDataSource = [NSMutableArray array];
        CaseHistoryModel *model = [[CaseHistoryModel alloc]init];
        model.name = @"周日";
        model.select = NO;
        [_arrDataSource addObject:model];
        CaseHistoryModel *model1 = [[CaseHistoryModel alloc]init];
        model1.name = @"周一";
        model1.select = NO;
        [_arrDataSource addObject:model1];
        CaseHistoryModel *model2 = [[CaseHistoryModel alloc]init];
        model2.name = @"周二";
        model2.select = NO;
        [_arrDataSource addObject:model2];
        CaseHistoryModel *model3 = [[CaseHistoryModel alloc]init];
        model3.name = @"周三";
        model3.select = NO;
        [_arrDataSource addObject:model3];
        CaseHistoryModel *model4 = [[CaseHistoryModel alloc]init];
        model4.name = @"周四";
        model4.select = NO;
        [_arrDataSource addObject:model4];
        CaseHistoryModel *model5 = [[CaseHistoryModel alloc]init];
        model5.name = @"周五";
        model5.select = NO;
        [_arrDataSource addObject:model5];
        CaseHistoryModel *model6 = [[CaseHistoryModel alloc]init];
        model6.name = @"周六";
        model6.select = NO;
        [_arrDataSource addObject:model6];
        if (_arrSelect.count > 0 ) {
            for (int i = 0; i < _arrSelect.count; i++) {
                NSString *str = _arrSelect[i];
                for (int j = 0; j < _arrDataSource.count; j++) {
                    CaseHistoryModel *model = _arrDataSource[j];
                    if ([str isEqualToString:model.name]) {
                        model.select = YES;
                        [_arrDataSource replaceObjectAtIndex:j withObject:model];
                        break;
                    }
                }
            }
        }
    }
    return _arrDataSource;
}


#pragma mark -


- (void)sureAction{
    NSMutableArray *arr = [NSMutableArray array];
    for (int i = 0 ; i < _arrDataSource.count; i++) {
        CaseHistoryModel *model = _arrDataSource[i];
        if (model.select) {
            [arr addObject:model.name];
        }
    }
    [self.deleaget backSelect:arr];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
